package core
{
	import core.customPupupManager.ICustomPopUpManager;
	import core.modelManager.IModelManager;
	import core.sceneManager.ISceneManager;
	import core.scriptManager.IScriptManager;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	
	import memory.MemoryRecovery;
	[Event(name="change_language", type="core.manager.MainManagerEvent")]
	public class MainManager extends EventDispatcher implements IMainManager
	{
        public static const CHINESE:String = "Chinese";
        public static const SMOOTH:String = "Smooth";
        public static const CLEAR:String = "Clear";
        public static const ENGLISH:String = "English";
        public static var LANGUAGE:String = "Chinese";
        public static var QUALITY:String = "Smooth";
		private static var _instance:MainManager;
		private var _stage:Stage;                                    //系统的舞台
		private var containers:Array=new Array();                    //整个系统的容器分三层,这个数组就是存储了这三个容器
		public const maxLayers:int=7;                                //系统最大的容器数。0层为3d场景,1,2层为2D场景，3层存放快照，4层为标准窗
                                                                     //5层为加载进度,6层系统信息和调试工具
		private var _sceneManager:ISceneManager;                     //场景管理者
		private var _modelManager:IModelManager;                     //数据库管理者
		public var customPopUpManager:ICustomPopUpManager;           //弹窗管理者
		public var scriptManager:IScriptManager;                     //脚本管理者
		public function MainManager()
		{
			if(_instance==null)
			{
				_instance=this;
			}else
			{
				throw new Error("MainManager不能被实例化.");
			}
		}
		public static function getInstance():MainManager
		{
			if(_instance==null) return new MainManager();
			return _instance;
		}
		/**
		 * 初始化容器
		 */ 
		private function initContainers():void
		{
			for(var i:int=0;i<maxLayers;i++)
			{
				var container:Sprite=new Sprite();
				this.stage.addChild(container);
				this.containers.push(container);
			}
		}
		/**
		 * 给场景舞台赋值
		 */ 
		public function set stage(obj:Stage):void
		{
			if(_stage==null)
			{
				 _stage=obj;
			}else
			{
				trace("stage已经存在，不能再次赋值.");
			}
			initContainers();
		}
		/**
		 * 获取场景舞台
		 */ 
		public function get stage():Stage
		{
			return _stage;
		}
		/**
		 * 获取存储的容器
		 */ 
		public function getContainer(id:int):Sprite
		{
			if(id<0)
			{
				id=0;
				trace("层次小于0，自动选取最底层");
			}
			if(id>=maxLayers)
			{
				id=2;
				trace("所要的层次太大，自动选取最高层");
			}
			return containers[id];
		}
		/**
		 * 根据语言执行相应的操作
		 */ 
		public function handleLanguageChange(funCh:Function,funEng:Function):void
		{
			if(MainManager.LANGUAGE==MainManager.CHINESE)
			{
				if(funCh!=null) funCh();
			}else
			{
				if(funEng!=null) funEng();
			}
			this.addEventListener(MainManagerEvent.CHANGE_LANGUAGE,function handleLanguageChange(e:MainManagerEvent):void{
				if(funCh==null || funEng==null) MemoryRecovery.getInstance().gcFun(MainManager.getInstance(),MainManagerEvent.CHANGE_LANGUAGE,handleLanguageChange);
				if(MainManager.LANGUAGE==MainManager.CHINESE)
				{
					funCh();
				}else
				{
					funEng();
				}
			});
		}
		///////////////////////////////////////////////////////下面的可以不用看//////////////////////////////////////////
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type,listener,useCapture);
		}
		public function get modelManager():IModelManager
		{
			return _modelManager;
		}
		public function set modelManager(modelManager:IModelManager):void
		{
			this._modelManager=modelManager;
		}
		public function get sceneManager():ISceneManager
		{
			return _sceneManager;
		}
		public function set sceneManager(sceneManager:ISceneManager):void
		{
			this._sceneManager=sceneManager;
		}
	}
}